import loadJson from "../../utils/loadJson";
import ImageTilesData from "./image_tiles_data";
import GeoJsonData from "./geojson_data";
import KML_data from "./kml_data";
import Tiles_data from "./tiles_data";
import Model_data from "./model_data";
import string from "shapefile/dbf/string";

function LayerManager(viewer) {
    this._viewer = viewer;
    this._jsonData = [];
    this.pathObject = null;
}

LayerManager.__ginstance = null;
LayerManager.GEOJSON = "GEOJSON";
LayerManager.KML = "KML";
LayerManager.GLTF = "GLTF";
LayerManager.TILESET = "TILESET";
LayerManager.IMGPAK = "IMGPAK";
LayerManager.GEOJSON_POLYGON = "MultiPolygon";
LayerManager.GEOJSON_POLYLINE = "LineString";
LayerManager.GEOJSON_POINT = "Point";
LayerManager.RootPath = "/storage/emulated/0/";
LayerManager.imgPrefix = "http://img.pak.mev/";
LayerManager.demPrefix = "http://dem.pak.mev/";
//新建文件模板
LayerManager.LayerTemplate = [
    {
        name: "影像数据",
        children: []
    },
    {
        name: "DEM数据",
        children: []
    },
    {
        name: "矢量数据",
        children: []
    },
    {
        name: "模型数据",
        children: []
    }
];

LayerManager.getInstance = function() {
    if (LayerManager.__ginstance == null) {
        LayerManager.__ginstance = new LayerManager(window.cesiumViewer);
    }
    return LayerManager.__ginstance;
};

/*读取图层文件并转换成json对象给每条数据赋加相应类型的数据对象*/
LayerManager.prototype.init = function(path,callback) {
    let that = this;
    if(!path){
        path = "./js/data.map";
    }
    loadJson( path, function(data) {
        data.forEach(function(g) {
            let index = 0;
            g.children.forEach(function(item) {
                item.index = index;
                index += 1;
                item.cesiumTreeData = that.getCesiumTreeData(item);
            });
        });
        that._jsonData = data;
        that.loadData(data);
        if(callback){
            callback(data);
        }

    });
};
//给每条数据根据各自类型生成相应的数据对象
LayerManager.prototype.getCesiumTreeData = function(item) {
    switch (item.class) {
        case LayerManager.IMGPAK:
            return new ImageTilesData(item, this._viewer);
            break;
        case LayerManager.GEOJSON:
            return new GeoJsonData(item, this._viewer);
            break;
        case LayerManager.TILESET:
            return new Tiles_data(item, this._viewer);
            break;
        case LayerManager.GLTF:
            return new Model_data(item, this._viewer);
            break;
        case LayerManager.KML:
            return new KML_data(item, this._viewer);
            break;
    }
};



//将对象中.visible为true都进行展示
LayerManager.prototype.loadData = function(data) {
    data.forEach(function(g) {
        g.children.forEach(function(item) {
            if (item.visible === true) {
                if (item.cesiumTreeData) {
                    item.cesiumTreeData.show();
                }
            }
        });

























    });
};
//将所有数据全部关闭展示
LayerManager.prototype.closeLoadData = function() {
    let d = this._jsonData;
    d.forEach(function(g) {
        g.children.forEach(function(item) {
            item.cesiumTreeData.setVisible(false);
        });
    });
};
//获得图层对象
LayerManager.prototype.getLayerData = function() {
    return this._jsonData;
};
//将对象属性进行设置
LayerManager.prototype.setProperties = function(item, props) {
    item.cesiumTreeData.setProperties(props);
};
//保存图层对象保存到文件里
LayerManager.prototype.saveFile = function(that, layerData) {
    that.$message({
        showClose: true,
        message: "已保存至" + this.pathObject,
        type: "warning"
    });
    this._jsonData = layerData;

    let resultLayer = [];
    layerData.forEach(function(g) {
        let a = {};
        a.name = g.name;
        a.children = [];
        g.children.forEach(function(item) {
            let obj = {};
            obj.datatype = item.datatype;
            obj.id = item.id;
            obj.name = item.name;

            //使用SdcardNumber代替字符
            if (string(obj.datatype) === "IMGPAK" || string(obj.datatype) === "DEM数据") {
                const str = item.path.split("/");
                const index = str[5];
                str[5] = "SdcardNumber";
                item.path = str[0] + "/" + str[1] + "/" + str[2] + "/" + str[3] + "/" + str[4] + "/" + str[5] + "/" + str[6] + "/" + str[7] + "/" + str[8] + "/" + str[9];
            } else {
                const str1 = item.path.split("/");
                const index1 = str1[2];
                str1[2] = "SdcardNumber";
                item.path = str1[0] + "/" + str1[1] + "/" + str1[2] + "/" + str1[3] + "/" + str1[4] + "/" + str1[5] + "/" + str1[6];
            }

            obj.path = item.path;
            obj.visible = item.cesiumTreeData._data.visible;
            if (item.cesiumTreeData.properties) {
                obj.properties = item.cesiumTreeData.properties;
            }
            a.children.push(obj);
        });
        resultLayer.push(a);
    });
    /*resultLayer.forEach(function(g) {
    g.children.forEach(function(item) {
        if (item.datatype === LayerManager.GEOJSON) {
            if (item.properties.type) {
                delete item.properties.type;
            }
        }
    });
    });*/
    JavaContext.saveFile(this.pathObject, JSON.stringify(resultLayer));
};

//替換sd号码
LayerManager.prototype.saveSdcard = function(that, layerData, sdcard) {
    this._jsonData = layerData;
    let resultLayer = [];
    layerData.forEach(function(g) {
        let a = {};
        a.name = g.name;
        a.children = [];
        g.children.forEach(function(item) {
            let obj = {};
            obj.datatype = item.datatype;
            obj.id = item.id;
            obj.name = item.name;

            //使用SdcardNumber代替字符
            if (string(obj.datatype) === "IMGPAK" || string(obj.datatype) === "DEM数据") {
                const str = item.path.split("/");
                const index = str[5];
                str[5] = sdcard;
                item.path = str[0] + "/" + str[1] + "/" + str[2] + "/" + str[3] + "/" + str[4] + "/" + str[5] + "/" + str[6] + "/" + str[7] + "/" + str[8] + "/" + str[9];
            } else {
                const str1 = item.path.split("/");
                const index1 = str1[2];
                str1[2] = sdcard;
                item.path = str1[0] + "/" + str1[1] + "/" + str1[2] + "/" + str1[3] + "/" + str1[4] + "/" + str1[5] + "/" + str1[6];
            }

            obj.path = item.path;
            obj.visible = item.cesiumTreeData._data.visible;
            if (item.cesiumTreeData.properties) {
                obj.properties = item.cesiumTreeData.properties;
            }
            a.children.push(obj);
        });
        resultLayer.push(a);
    });
    /*resultLayer.forEach(function(g) {
        g.children.forEach(function(item) {
            if (item.datatype === LayerManager.GEOJSON) {
                if (item.properties.type) {
                    delete item.properties.type;
                }
            }
        });
    });*/
    JavaContext.saveFile(this.pathObject, JSON.stringify(resultLayer));
};
//更新图层对象
LayerManager.prototype.updateJsonData = function(layerData) {
    let that = this;
    layerData.forEach(function(g) {
        g.children.forEach(function(item) {
            if (!item.cesiumTreeData) {
                item.cesiumTreeData = that.getCesiumTreeData(item);
            }
        });
    });
    that._jsonData = layerData;
};

//给pathObject赋值
LayerManager.prototype.setPathObject = function(path) {
    this.pathObject = path;
};

LayerManager.prototype.getPathObject = function() {
    return this.pathObject;
};
LayerManager.prototype.getVisibleLineLayer = function() {
    let ls = [];
    this._jsonData.forEach(function(g) {
        g.children.forEach(function(item) {
            if (item.cesiumTreeData) {
                if (item.cesiumTreeData._data.datatype === LayerManager.GEOJSON && item.cesiumTreeData._data.visible) {
                    if (item.cesiumTreeData._vectorType === LayerManager.GEOJSON_POLYLINE) {
                        ls.push(item);
                    }
                }
            }
        });
    });
    return ls;
};

export default LayerManager;
